package com.boatAnchorAssistant.service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boatAnchorAssistant.pojo.entity.DataDictionary;
import com.boatAnchorAssistant.pojo.entity.DataDictionaryType;
import com.boatAnchorAssistant.pojo.entity.User;
import com.boatAnchorAssistant.pojo.param.dataDictionaryType.*;
import com.boatAnchorAssistant.pojo.vo.dataDictionaryType.*;
import com.boatAnchorAssistant.pojo.dto.dataDictionaryType.*;
import com.boatAnchorAssistant.service.repository.in.DataDictionaryRepository;
import com.boatAnchorAssistant.service.repository.in.DataDictionaryTypeRepository;
import com.boatAnchorAssistant.service.repository.in.UserRepository;
import com.boatAnchorAssistant.service.service.in.DataDictionaryTypeService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.exceptions.BizException;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：数据字典类型服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 *
 */
@Service
public class DataDictionaryTypeServiceImpl implements DataDictionaryTypeService {

    private final DataDictionaryTypeRepository dataDictionaryTypeRepository;

    private final UserRepository userRepository;

    private final DataDictionaryRepository dataDictionaryRepository;

    public DataDictionaryTypeServiceImpl(DataDictionaryTypeRepository dataDictionaryTypeRepository, UserRepository userRepository, DataDictionaryRepository dataDictionaryRepository) {
        this.dataDictionaryTypeRepository = dataDictionaryTypeRepository;
        this.userRepository = userRepository;
        this.dataDictionaryRepository = dataDictionaryRepository;
    }


    /**
     * 创建数据字典类型
     *
     * @param param
     */
    @Override
    public void createDataDictionaryType(CreateDataDictionaryTypeParam param) {
        DataDictionaryType dataDictionaryType = new DataDictionaryType();
        BeanUtils.copyProperties(param, dataDictionaryType);
        Long id = IdGenerator.SNOW.generate();
        dataDictionaryType.setId(id);
        dataDictionaryTypeRepository.create(dataDictionaryType);
    }

    /**
     * 修改数据字典类型
     *
     * @param param
     */
    @Override
    public void editDataDictionaryType(EditDataDictionaryTypeParam param) {
        Long id = param.getId();
        DataDictionaryType dataDictionaryType = dataDictionaryTypeRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, dataDictionaryType);
        dataDictionaryTypeRepository.edit(dataDictionaryType);
    }

    /**
     * 移除数据字典类型
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeDataDictionaryType(Long id) {
        dataDictionaryTypeRepository.removeById(id);

        DataDictionary t = new DataDictionary();
        t.setType(id);
        List<DataDictionary> list = dataDictionaryRepository.getListByEntity(t);
        if (!CollectionUtils.isEmpty(list)){
            throw new BizException("数据字典类型下还有值为清空");
        }
    }

    /**
     * 启动/禁用数据字典类型
     *
     * @param id
     */
    @Override
    public void enabledDataDictionaryType(Long id) {
        DataDictionaryType dataDictionaryType = dataDictionaryTypeRepository.getByIdCheck(id);
        dataDictionaryType.setIsDisable(!dataDictionaryType.getIsDisable());
        dataDictionaryTypeRepository.edit(dataDictionaryType);
    }

    /**
     * 获取数据字典类型详情
     *
     * @param id
     * @return
     */
    @Override
    public GetDataDictionaryTypeVo getDataDictionaryType(Long id) {
        DataDictionaryType dataDictionaryType = dataDictionaryTypeRepository.getByIdCheck(id);
        GetDataDictionaryTypeVo vo = new GetDataDictionaryTypeVo();
        BeanUtils.copyProperties(dataDictionaryType, vo);
        return vo;
    }

    /**
     * 获取数据字典类型 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetDataDictionaryTypeListVo> getDataDictionaryTypeList(GetDataDictionaryTypeListParam param) {

        SelectDataDictionaryTypeDto dto = new SelectDataDictionaryTypeDto();
        BeanUtils.copyProperties(param, dto);
        Page<DataDictionaryType> page = dataDictionaryTypeRepository.selectDataDictionaryType(dto);
        List<DataDictionaryType> dataDictionaryTypeList = page.getRecords();

        List<Long> userIds =new ArrayList<>();
        List<Long> createIds = dataDictionaryTypeList.stream()
                .map(DataDictionaryType::getCreator)
                .collect(Collectors.toList());

        List<Long> updateIds = dataDictionaryTypeList.stream()
                .map(DataDictionaryType::getUpdater)
                .collect(Collectors.toList());



        userIds.addAll(createIds);
        userIds.addAll(updateIds);

        Map<Long, String> userNameMap = userRepository.getListByIds(updateIds)
                .stream()
                .collect(Collectors.toMap(User::getId, User::getName));


        List<GetDataDictionaryTypeListVo> list = new ArrayList<>();
        for (DataDictionaryType dataDictionaryType : dataDictionaryTypeList) {
            GetDataDictionaryTypeListVo vo = new GetDataDictionaryTypeListVo();
            BeanUtils.copyProperties(dataDictionaryType, vo);

            vo.setCreateName(userNameMap.get(dataDictionaryType.getCreator()));
            vo.setUpdateName(userNameMap.get(dataDictionaryType.getUpdater()));

            vo.setEnabled(!dataDictionaryType.getIsDisable());

            list.add(vo);
        }
        return new PageResult<>(list, page);

    }

    /**
     * 获取数据字典类型下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetDataDictionaryTypeDownListVo> getDataDictionaryTypeDownList(GetDataDictionaryTypeDownListParam param) {
        DataDictionaryType po = new DataDictionaryType();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<DataDictionaryType> dataDictionaryTypeList = dataDictionaryTypeRepository.getListByEntity(po);

        List<GetDataDictionaryTypeDownListVo> list = new ArrayList<>();
        for (DataDictionaryType dataDictionaryType : dataDictionaryTypeList) {
            GetDataDictionaryTypeDownListVo vo = new GetDataDictionaryTypeDownListVo();
            BeanUtils.copyProperties(dataDictionaryType, vo);
            list.add(vo);
        }
        return list;
    }


}



